<template>
	<view>
		<button @click="grant()"> 向用户申请位置权限</button>
		<button @click="getSite()"> 获取位置信息</button>
		<button @click="chooseImage()">选择图片</button>
		<button @click="openFlashLigth()">打开系统闪光灯</button>
		<button @click="callPhone()">拨打电话</button>
		<button @click="ohosShare()">分享</button>
		<button @click="playAudio()">播放本地语音</button>
		<button @click="playMultiAudio()">播放多段语音</button>
		<button @click="requestPermisson()"> 申请相机权限</button>
		
		<image :src="ImageSrc" style="width: 100rpx; height: 100rpx;"></image>
	</view>
</template>

<script>
	import AddressUtil from '@/uni_modules/my-getaddress';
	import FlashUtil from '@/uni_modules/my-flashmgr';
	import ResourceUtil from '@/uni_modules/my-resource'
	export default {
		data() {
			return {
				ImageSrc: ''
			}
		},
		methods: {
			async grant() {
				let location = await AddressUtil.grantLocationPermission();
			},

			async getSite() {
				let location = await AddressUtil.getAddress();
				uni.showModal({
					title: JSON.stringify(location.placeName)
				})
				console.log(JSON.stringify(location))
			},

			chooseImage() {
				uni.chooseImage({
					success: (res) => {
						this.ImageSrc = res.tempFilePaths[0]
					}
				})
			},

			openFlashLigth() {
				FlashUtil.SetTorchMode(true);

				setTimeout(() => {
					FlashUtil.SetTorchMode(false)
				}, 2000)
			},

			ohosShare() {
				uni.shareWithSystem({
					summary: '',
					href: 'https://uniapp.dcloud.io',
					success() {
						// 分享完成，请注意此时不一定是成功分享
					},
					fail() {
						// 分享失败
					}
				})
			},
			callPhone() {
				uni.makePhoneCall({
					phoneNumber: '114' //仅为示例
				});
			},
			async playAudio() {
				let innerAudioContext = uni.createInnerAudioContext();
				innerAudioContext.autoplay = true;

				// 方案一 通过 uts插件 获取 resfile 路径，再拼接获得文件路径
				// let tempPath = await ResourceUtil.getFilePath();
				// let url = tempPath + '/apps/HBuilder/www/static/yuyin.mp3';

				// 方案二 自行拼接文件路径
				// uniapp项目的 static 目录 对应 /data/storage/el1/bundle/entry/resources/resfile/apps/HBuilder/www/static/
				let url = '/data/storage/el1/bundle/entry/resources/resfile/apps/HBuilder/www/static/yuyin.mp3';

				innerAudioContext.src = url;
				innerAudioContext.onError((res) => {
					console.log('msg: ', res.errMsg);
					console.log('code: ', res.errCode);
				})
				innerAudioContext.onPlay(() => {
					console.log('开始播放');
				});

				innerAudioContext.onEnded(() => {
					innerAudioContext.destroy();
					innerAudioContext = null;
				})
			},

			async playMultiAudio() {
				const url = [
					'/data/storage/el1/bundle/entry/resources/resfile/apps/HBuilder/www/static/yuyin.mp3',
					'/data/storage/el1/bundle/entry/resources/resfile/apps/HBuilder/www/static/sample-6s.mp3'
				];

				await this.PlayAudio(url[0]);
				await this.PlayAudio(url[1]);

			},

			async PlayAudio(url) {
				return new Promise((resolve, rej) => {
					let innerAudioContext = uni.createInnerAudioContext();
					innerAudioContext.autoplay = true;
					innerAudioContext.src = url;
					innerAudioContext.onPlay(() => {
						console.log('开始播放');
					});
					innerAudioContext.onEnded(() => {
						console.log('播放完毕')
						innerAudioContext.destroy();
						innerAudioContext = null;
						resolve('done')
					})
				})
			},
			async requestPermisson() {
				let res = await FlashUtil.grantCameraPermission()
			}

		}
	}
</script>

<style>

</style>